import { DeptInterface } from '@/api/interface/dept/dept';
import { RosterInterface } from '@/api/interface/personnel/roster/roster';
import { BasicForm } from '@/components/ProForm/interface';
import { PublicInterface } from '@/interface/public';

/**
 * 切换部门
 */

export function useChangeDeptForm(proTableBox: PublicInterface.ProTableValue) {
  let changeDeptForm = reactive<BasicForm<RosterInterface.ReqRosterChangeJob>>({
    form: {
      id: null,
      entryDeptId: null,
      entryJobId: null
    },
    title: '',
    minWidth: 660,
    labelWidth: [100, 120],
    visible: false,
    formAttrs: {
      labelWidth: '120px'
    },
    disabledPropList: ['entryJobId'],
    options: [
      {
        type: 'input',
        label: 'ID',
        prop: 'id',
        hide: true
      },
      {
        type: 'select',
        hide: false,
        prop: 'entryDeptId',
        label: '入职部门',
        children: [],
        rules: [
          {
            required: true,
            message: '入职部门',
            trigger: 'blur'
          }
        ],
        changeMethod: form => {
          const flag = !!form.entryDeptId;
          formChangeItemStatus(changeDeptForm, changeDeptForm.disabledPropList, flag);
          form.entryJobId = null;
          positionListFindTreeByDeptId({ deptId: form.entryDeptId }).then(data => {
            let children = new Map();
            children.set('entryJobId', arrTransitionSelectData(data, 'name', 'id'));
            formSetChildren(changeDeptForm, children);
          });
          // let transitionArr: transitionProp<RosterInterface.ReqRosterChangeJob>[] = [
          //   {
          //     oldField: form.entryDept,
          //     newField: 'entryJob'
          //   }
          // ];
          // setDictionaries(changeDeptForm, transitionArr);
        }
      },
      {
        type: 'select',
        hide: false,
        prop: 'entryJobId',
        label: '入职岗位',
        children: [],
        rules: [
          {
            required: true,
            message: '入职岗位',
            trigger: 'blur'
          }
        ]
      }
    ]
  });

  /**
   * @description: 保存表单数据
   * @return {*}
   */
  const saveAddChangeDeptForm = async (form: RosterInterface.ReqRosterChangeJob) => {
    await rosterChangeJob(form);

    changeDeptForm.visible = false;
    elMessage();
    proTableBox.value?.getTableList();
  };

  /**设置部门下拉数据 */
  const setDeptChildren = async (deptId: number) => {
    const params = { status: 1 };
    let data = [];
    if (deptId) {
      data.push(positionListFindTreeByDeptId({ deptId }));
    }

    const res: any = await promiseAllApi([deptListByFlattenTree(params), ...data]);

    const children = new Map();

    children.set('entryDeptId', arrTransitionTreeData<DeptInterface.ResListByTree>(res[0], 'name', 'id'));

    if (deptId) {
      children.set('entryJobId', arrTransitionTreeData(res[1], 'name', 'id'));
    }
    formSetChildren(changeDeptForm, children);
  };

  return { changeDeptForm, saveAddChangeDeptForm, setDeptChildren };
}
